package cn.sky.demo.controller;

import cn.sky.demo.aspect.Log;
import cn.sky.demo.domain.ResponseResult;
import cn.sky.demo.domain.dto.AddUserDto;
import cn.sky.demo.domain.dto.LoginUserDto;
import cn.sky.demo.domain.dto.UpdateUserDto;
import cn.sky.demo.domain.dto.RegistryUserDto;
import cn.sky.demo.enums.BusinessType;
import cn.sky.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * @author sky
 * @date 2025年09月15日 16:56
 * @description
 * @since 1.0.0
 */
@RestController
@RequestMapping("/user")
@Validated
public class User1Controller{
    @Autowired
    private UserService userService;

    @PostMapping
    @Log(title = "新增用户信息", businessType = BusinessType.INSERT)
    public ResponseResult addUser(@RequestBody AddUserDto addUserDto) {
        return userService.addUser(addUserDto);
    }
    @PutMapping
    public ResponseResult updateUser(@RequestBody UpdateUserDto updateUserDto) {
        return userService.updateUser(updateUserDto);
    }
    @DeleteMapping("/{id}")
    public ResponseResult deleteUser(@PathVariable("id") Long id) {
        return userService.deleteUser(id);
    }
    @Log(title = "获取用户信息", businessType = BusinessType.OTHER)
    @GetMapping
    @CrossOrigin // 3、SpringBoot 通过注解解决跨域
    public ResponseResult getUser(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        return userService.getUser(pageNum, pageSize);
    }

    @PostMapping("/registry")
    public ResponseResult registryUser(@Valid @RequestBody RegistryUserDto registryUserDto) {
        return userService.registryUser(registryUserDto);
    }
    @PostMapping("/registry1")
    public ResponseResult registryUser1(@NotBlank(message = "name不能为空") String name) {
        return ResponseResult.okResult(name);
    }
    @PostMapping("/login")
    public ResponseResult login(@RequestBody LoginUserDto loginUserDto) {
        return userService.login(loginUserDto);
    }
}
